﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;

using Gift.Model;
using Gift.DAL;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;

namespace WebApp.admin.goods
{
    public partial class pointsExchange : BaseAdmin
    {

        tbPointsFor oPointsFor = new tbPointsFor();


        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                this.oPointsFor.name = this.txtName.Value;
                this.oPointsFor.ifGift = this.rdIfGift1.Checked ;
                this.oPointsFor.exchangePoints = Convert.ToInt32(this.txtExchangePoints.Value);
                this.oPointsFor.descript = this.fckDescript.Value;
              
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    tbPointsFor pointsForById = BaseFactory.thePointsFor.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                    if (this.filePointsFor.PostedFile.FileName != "")
                    {
                        this.oPointsFor.image = "/cn/points_for/" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + Path.GetExtension(this.filePointsFor.PostedFile.FileName);
                        File.Delete(base.Server.MapPath(pointsForById.image));
                    }
                    else
                    {
                        this.oPointsFor.image = pointsForById.image;
                    }
                    this.oPointsFor.id = Convert.ToInt32(base.Request.QueryString["id"]);
                    if (BaseFactory.thePointsFor.Update(this.oPointsFor))
                    {
                        if (this.filePointsFor.PostedFile.FileName != "")
                        {
                            this.filePointsFor.PostedFile.SaveAs(base.Server.MapPath(this.oPointsFor.image));
                        }
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"pointsExchangeList.aspx\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                    }
                }
                else
                {
                    this.oPointsFor.image = "/cn/points_for/" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + Path.GetExtension(this.filePointsFor.PostedFile.FileName);
                    this.oPointsFor.CompanyId = int.Parse(base.Administrator.CompanyId);
                    if (BaseFactory.thePointsFor.Add(this.oPointsFor) > 0)
                    {
                        if (this.filePointsFor.PostedFile.FileName != "")
                        {
                            this.filePointsFor.PostedFile.SaveAs(base.Server.MapPath(this.oPointsFor.image));
                        }
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"pointsExchangeList.aspx\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                    }
                }
            }
            catch
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }
        }

        public StringBuilder GetTitle()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if (base.Request.QueryString["ot"] == null)
                {
                    return builder;
                }
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    builder.Append("编辑商品");
                    return builder;
                }
                builder.Append("添加商品");
            }
            catch
            {
            }
            return builder;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (base.Request.QueryString["ot"] == "ed")
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.PointsExchangeEdit);
                }
                else
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.PointsExchangeAdd);
                }
                if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
                {
                    if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                    {
                        this.PointsForBind();
                    }
                    else
                    {
                        this.imgPointsFor.Style.Add("display", "none");
                        this.rdIfGift0.Checked = true;
                    }
                }
            }
        }

        private void PointsForBind()
        {
            try
            {
                tbPointsFor pointsForById = BaseFactory.thePointsFor.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                if (pointsForById != null)
                {

                    this.txtName.Value = pointsForById.name;
                    if (pointsForById.ifGift)
                    {
                        this.rdIfGift1.Checked = true;
                    }
                    else
                    {
                        this.rdIfGift0.Checked = true;
                    }
                    this.imgPointsFor.Src = pointsForById.image;
                    this.txtExchangePoints.Value = pointsForById.exchangePoints.ToString();
                    this.fckDescript.Value = pointsForById.descript;
                }
            }
            catch
            {
            }
        }
    }
}